package com.sz.common.core.system.service.impl;

import com.sz.common.base.exception.BizException;
import com.sz.common.base.service.AbstractService;
import com.sz.common.base.utils.EntityUtils;
import com.sz.common.base.utils.StringUtils;
import com.sz.common.core.system.entity.UnitConversion;
import com.sz.common.core.system.service.UnitConversionService;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * Function: ApiCostServiceImpl. <br>
 * Date: 2017年02月14日 11:18 <br>
 * Author: wentao.chang
 */
@Service
public class UnitConversionServiceImpl extends AbstractService implements UnitConversionService {

    @Override
    protected String getMapperNamespace() {
        return "";
    }

    @Override
    public List<UnitConversion> findByType(String type) {
        return dao.findForList("UnitConversionMapper.findByType", type, UnitConversion.class);
    }

    /**
     * 重量换算
     */
    @Override
    public BigDecimal getTargetWeight(BigDecimal weight, String fromUnit, String toUnit) {
        if (StringUtils.isEmpty(fromUnit)) {
            throw new BizException("fromUnit is null");
        }
        if (StringUtils.isEmpty(toUnit)) {
            throw new BizException("toUnit is null");
        }
        List<UnitConversion> unitConversions = findByType(UnitConversionService.WEIGHT);
        Map<String, UnitConversion> unitConversionMap = EntityUtils.getStringKeyMapFromBeans(unitConversions, "code");
        UnitConversion ucWeight = unitConversionMap.get(fromUnit);
        if (ucWeight == null) {
            throw new BizException("fromUnit conversion " + fromUnit + " is null");
        }
        UnitConversion ucSetting = unitConversionMap.get(toUnit);
        if (ucSetting == null) {
            throw new BizException("toUnit conversion " + toUnit + " is null");
        }
        BigDecimal standardWeigh = weight.divide(ucWeight.getValue(), 10, BigDecimal.ROUND_HALF_UP);
        return standardWeigh.multiply(ucSetting.getValue());
    }

    /**
     * 长度换算
     */
    @Override
    public BigDecimal getTargetLength(BigDecimal length, String fromUnit, String toUnit) {
        if (StringUtils.isEmpty(fromUnit)) {
            throw new BizException("fromUnit is null");
        }
        if (StringUtils.isEmpty(toUnit)) {
            throw new BizException("toUnit is null");
        }
        List<UnitConversion> unitConversions = findByType(UnitConversionService.LENGTH);
        Map<String, UnitConversion> unitConversionMap = EntityUtils.getStringKeyMapFromBeans(unitConversions, "code");
        UnitConversion ucWeight = unitConversionMap.get(fromUnit);
        if (ucWeight == null) {
            throw new BizException("fromUnit conversion " + fromUnit + " is null");
        }
        UnitConversion ucSetting = unitConversionMap.get(toUnit);
        if (ucSetting == null) {
            throw new BizException("toUnit conversion " + toUnit + " is null");
        }
        BigDecimal standardWeigh = length.divide(ucWeight.getValue(), 10, BigDecimal.ROUND_HALF_UP);
        return standardWeigh.multiply(ucSetting.getValue());
    }
}

